<template>
  <div>
    <el-card>
      <el-row style="margin-bottom: 10px">
        <el-col :span="3" style="margin-right: 10px">
          <el-select v-model="selectStatus" placeholder="审核状态" value="">
            <el-option label="" value=""></el-option>
            <el-option label="未审核" value="0"></el-option>
            <el-option label="未通过" value="1"></el-option>
            <el-option label="通过" value="2"></el-option>
          </el-select>
        </el-col>
        <el-col span="auto" style="margin-right: 10px">
          <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData" border>
        <el-table-column label="ID" prop="id" header-align="center" align="center" sortable></el-table-column>
        <el-table-column label="帖子ID" prop="postId" header-align="center" align="center"></el-table-column>
        <el-table-column label="描述" prop="description" header-align="center" align="center"></el-table-column>
        <el-table-column label="审核状态" prop="reviewStatus" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.reviewStatus === 0" type="warning">未审核</el-tag>
            <el-tag v-if="scope.row.reviewStatus === 1" type="danger">未通过</el-tag>
            <el-tag v-if="scope.row.reviewStatus === 2" type="success">通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="gmtCreate" header-align="center" align="center" sortable></el-table-column>
        <el-table-column label="修改时间" prop="gmtModified" header-align="center" align="center" sortable></el-table-column>
        <el-table-column label="操作" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleOpenEdit(scope.$index, scope.row)"
              style="margin-right: 10px" v-show="scope.row.reviewStatus === 0">审核</el-button>
            <el-button size="mini" type="primary" @click="OpenPost(scope.$index, scope.row)" style="margin-right: 10px"
              v-show="scope.row.reviewStatus !== 0">查看</el-button>
            <el-button size="mini" @click="deleteDialogOpen(scope.row)" type="danger">删除</el-button>
            <el-dialog title="确认删除" :visible.sync="deleteDialog" width="35%">
              <span slot="footer" class="dialog-footer">
                <el-button @click="deleteDialog = false">取 消</el-button>
                <el-button type="primary" @click="handleDelete(scope.$index, scope.row)">确 定</el-button>
              </span>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="审核帖子" :visible.sync="dialogue" width="50%">
        <el-form>
          <PostDetail :postId="postCommitForm.postId" :key="postCommitForm.postId"></PostDetail>
          <el-form-item label="审核" prop="reviewStatus">
            <el-radio-group v-model="postCommitForm.reviewStatus">
              <el-radio :label="1" border>退回</el-radio>
              <el-radio :label="2" border>通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审核结果描述" prop="description">
            <el-input v-model="postCommitForm.description" type="textarea" placeholder="请输入内容"
              prefix-icon="el-icon-edit"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogue = false">取 消</el-button>
          <el-button type="primary" @click="handleEdit">确 定</el-button>
        </span>
      </el-dialog>
      <el-dialog title="帖子" :visible.sync="findPostDialog" width="50%">
        <el-form>
          <PostDetail :postId="postCommitForm.postId" :key="postCommitForm.postId"></PostDetail>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="findPostDialog = false">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 底部分页 -->
      <div style="text-align: center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="currentPage" :page-sizes="[10, 15, 20]" :page-size="pageSize" :total="totalSize" background
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import jsCookie from 'js-cookie'
import { getNotReviewList, editNotReview, deleteNotReview } from '@/api/post/notReviewed'
import PostDetail from './components/PostDetail.vue'

export default {
  provide() {
    return {
      getDataList: this.getDataList,
    }
  },
  components: { PostDetail },
  data() {
    return {
      userId: 0,
      dataList: [],
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      dialogue: false,
      deleteDialog: false,
      findPostDialog: false,
      statusType: ['未审核'],
      form: {
        id: 0,
        description: "",
        postId: 0,
        reviewStatus: 0,
        gmtCreate: '',
        gmtModified: ''
      },
      postCommitForm: {
        id: 0,
        userId: 0,
        description: "",
        postId: 0,
        reviewStatus: 0
      },
      selectStatus: ''
    }
  },
  methods: {
    handleOpenEdit(index, row) {
      this.postCommitForm.postId = row.postId
      this.dialogue = true
      this.init(row)
    },
    deleteDialogOpen(row) {
      this.deleteDialog = true
      this.init(row)
    },
    OpenPost(index, row) {
      this.postCommitForm.postId = row.postId
      this.findPostDialog = true
      this.init(row)
    },
    handleEdit(index, row) {
      editNotReview({
        "id": this.postCommitForm.id,
        "description": this.postCommitForm.description,
        "userId": this.postCommitForm.userId,
        "postId": this.postCommitForm.postId,
        "reviewStatus": this.postCommitForm.reviewStatus,
      }).then(res => {
        if (res.data.code === 200) {
          this.$message.success({
            message: res.data.message,
            duration: 1000
          })
          this.dialogue = false
          this.getDataList()
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    handleDelete() {
      deleteNotReview(this.form.id).then(res => {
        if (res.data.code === 200) {
          this.deleteDialog = false
          this.$message.success({
            message: res.data.message,
            duration: 1000
          })
          this.getDataList()
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    getDataList() {
      getNotReviewList({
        pageNo: this.currentPage,
        pageSize: this.pageSize,
      }).then(res => {
        if (res.data.code === 200) {
          this.dataList = res.data.pageResult.dataList
          this.tableData = this.dataList
          this.totalSize = res.data.pageResult.totalCount
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    init(data) {
      this.form = {
        id: data.id,
        description: data.description,
        postId: data.postId,
        reviewStatus: data.reviewStatus,
        gmtCreate: data.gmtCreate,
        gmtModified: data.gmtModified
      }
      this.postCommitForm = {
        id: data.id,
        userId: this.userId,
        postId: data.postId,
      }
    },
    search() {
      getNotReviewList({
        reviewStatus: this.selectStatus,
        pageNo: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        if (res.data.code === 200) {
          this.dataList = res.data.pageResult.dataList
          this.tableData = this.dataList
          this.totalSize = res.data.pageResult.totalCount
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    }
  },
  created() {
    this.userId = JSON.parse(jsCookie.get("user")).id
    this.getDataList()
  },
  watch: {
    searchKeyword(newVal) {
      if (newVal === '') {
        this.getDataList()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
}
</style>
